package cn.itcast.web.controller.packing;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.itcast.util.DownloadUtils;
import cn.itcast.domain.packing.Finance;
import cn.itcast.domain.packing.Invoice;
import cn.itcast.service.packing.FinanceService;
import cn.itcast.service.packing.InvoiceService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.terracotta.quartz.wrappers.FiredTrigger;

import java.io.ByteArrayOutputStream;
import java.util.Map;

@Controller
@RequestMapping(value = "/cargo/finance",name = "财务处理")
public class FinanceController extends BaseController {

    @Reference
    private FinanceService financeService;

    @RequestMapping(value = "/toFinance",name = "财务什么")
    public String toInvoice(String invoiceId ){

        request.setAttribute("invoiceId",invoiceId);

        return "packing/finance/finance-toFinance";
    }
    @RequestMapping(value = "/edit",name = "新增/修改")
    public String edit(Finance finance, String invoiceId ){

        if (StrUtil.hasEmpty(finance.getFinanceId())){
            finance.setFinanceId(invoiceId);
            financeService.save(finance);
        }else {
            financeService.update(finance);
        }
        return "redirect:/cargo/finance/list.do";
    }
    @RequestMapping(value = "list", name = "发票列表")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {

        PageInfo page = financeService.findByPage(pageNum, pageSize, null);
        request.setAttribute("page", page);
        return "packing/finance/finance-list";

    }
    @RequestMapping(value = "/toUpdate",name = "去修改页面")
    public String toUpdate( String financeId){
        Finance finance = financeService.findById(financeId);
        request.setAttribute("finance",finance);
        return "/packing/finance/finance-update";
    }


    @Reference
    private InvoiceService invoiceService;

    @RequestMapping(value = "/toState",name = "ajax发票")
    @ResponseBody
    public Integer toState(@RequestBody String id){
      return invoiceService.findById(id).getState();
    }

    @RequestMapping(value = "/delete",name = "删除")
    public String delete(String financeId){
        financeService.delete(financeId);

        return "redirect:/cargo/finance/list.do";
    }
    @RequestMapping(value = "/submit", name = "委托上报")
    public String submit(String financeId) {
        Finance finance = financeService.findById(financeId);
        finance.setState(1);
        financeService.update(finance);
        return "redirect:/cargo/finance/list.do";
    }

    @RequestMapping(value = "/cancel", name = "取消上报委托")
    public String cancel(String financeId) {
        Finance finance = financeService.findById(financeId);
        finance.setState(0);
        financeService.update(finance);
        return "redirect:/cargo/finance/list.do";
    }

    @RequestMapping(value = "/toPdf",name = "下载pdf")
    public void toPdf(String financeId) throws Exception {
        String realPath = session.getServletContext().getRealPath("/jasper/self/finance.jasper");

        Finance finance = financeService.findById(financeId);

        Map<String, Object> map = BeanUtil.beanToMap(finance, false, true);

        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, new JREmptyDataSource());

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        JasperExportManager.exportReportToPdfStream(jasperPrint,byteArrayOutputStream);

        DownloadUtils.download(byteArrayOutputStream,response,"finance.pdf");

    }
}
